package com.tao.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.tao.common.creo.Utils.ThreadLocalUtil;
import com.tao.common.creo.constants.CacheConstants;
import com.tao.common.creo.constants.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * mybatis-plus 自动填充字段
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createBy", Long.class, getLoginUserId());
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
    }



    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, "updateBy", Long.class, getLoginUserId());
    }

    private Long getLoginUserId() {
        Long userId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
        System.out.println("userId: " + userId);
        return userId;
    }
}
